//
//  ESTChartsTableViewCell.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/17.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTChartsTableViewCell.h"
#import "ESTActivityChartsModel.h"

@interface ESTChartsTableViewCell ()
@property (weak, nonatomic) IBOutlet UIButton *firstButton;
@property (weak, nonatomic) IBOutlet UIButton *secondButton;
@property (weak, nonatomic) IBOutlet UIButton *thirdButton;
@property (weak, nonatomic) IBOutlet UIButton *fourthButton;
/**
 *  选择哪一个
 */
@property(nonatomic, assign) NSInteger selectedNumber;
@end

@implementation ESTChartsTableViewCell
+ (instancetype)creatChartsTableViewCell:(UITableView *)tableView selectedNumber:(NSInteger)selectedNumber {
    ESTChartsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTChartsTableViewCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ESTChartsTableViewCell" owner:nil options:nil].lastObject;
    }
    cell.selectedNumber = selectedNumber + 2;
    return cell;
}
- (void)setupButtonImageInRight:(UIButton *)button {
    CGFloat imageWidth = CGRectGetWidth(button.imageView.bounds);
    CGFloat space = 0;
    CGSize size = [button.titleLabel.text getSizeWithFont:button.titleLabel.font];
    CGFloat titleWidth = size.width;
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imageWidth+space*2), 0, (imageWidth+space*2))];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, (titleWidth+space*2), 0, -(titleWidth+space*2))];
}
- (void)setModel:(ESTActivityChartsModel *)model {
    _model = model;
    self.secondTitle = model.EMP_NAME;
    self.thirdTitle = model.BUS_AMOUNT;
    self.fourthTitle = model.BUS_RT;
    if ([model.EMP_ID isEqualToString:UserInfo.userid]) {
        self.backgroundColor = TYZYellowColor;
    }
}
- (void)setFirstTitle:(NSString *)firstTitle {
    _firstTitle = firstTitle;
    [self.firstButton setTitle:firstTitle forState:UIControlStateNormal];
}
- (void)setSecondTitle:(NSString *)secondTitle {
    _secondTitle = secondTitle;
    [self.secondButton setTitle:secondTitle forState:UIControlStateNormal];
}
- (void)setThirdTitle:(NSString *)thirdTitle {
    _thirdTitle = thirdTitle;
    [self.thirdButton setTitle:thirdTitle forState:UIControlStateNormal];
    if ([thirdTitle isEqualToString:[@"数量" S]]) {
        if (self.selectedNumber == 2) {
            [self.thirdButton setImage:[UIImage imageNamed:@"charts_selected_yes"] forState:UIControlStateNormal];
        } else if (self.selectedNumber == 3) {
            [self.thirdButton setImage:[UIImage imageNamed:@"charts_selected_no"] forState:UIControlStateNormal];
        }
        [self setupButtonImageInRight:self.thirdButton];
    }
}
- (void)setFourthTitle:(NSString *)fourthTitle {
    _fourthTitle = fourthTitle;
    [self.fourthButton setTitle:fourthTitle forState:UIControlStateNormal];
    if ([fourthTitle isEqualToString:[@"转换率" S]]) {
        if (self.selectedNumber == 2) {
            [self.fourthButton setImage:[UIImage imageNamed:@"charts_selected_no"] forState:UIControlStateNormal];
        } else if (self.selectedNumber == 3) {
            [self.fourthButton setImage:[UIImage imageNamed:@"charts_selected_yes"] forState:UIControlStateNormal];
        }
        [self setupButtonImageInRight:self.fourthButton];
    }
}
- (IBAction)didClickFirstButton:(id)sender {
}
- (IBAction)didClickSecondButton:(id)sender {
}
- (IBAction)didClickThirdButton:(id)sender {
    if (self.didClickNumberBlock) {
        self.didClickNumberBlock();
    }
}
- (IBAction)didClickFourthButton:(id)sender {
    if (self.didClickPercentageBlock) {
        self.didClickPercentageBlock();
    }
}
@end
